package com.cloudm.technician.ui.modules.common.guide

import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.extensions.logger_e
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.ui.base.BaseFragment
import com.cloudm.technician.ui.modules.common.login.LoginActivity
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.fragment_guide.*

/**
 * 作者: wesley
 * 时间: 2017/12/13
 * 描述: ui
 */
class GuideFragment : BaseFragment<GuidePresenter>(), GuideContract.View {

    private var position = 0

    override val resource: Int
        get() = R.layout.fragment_guide

    companion object {
        private const val TAG = "GuideFragment"
        private const val POSITION = "position"
        fun newInstance(position: Int): GuideFragment = GuideFragment().apply {
            arguments = Bundle().apply {
                putInt(POSITION, position)
            }
        }
    }

    override fun initInject() {
        DaggerGuideComponent.builder().appComponent(appComponent).guidePresenterModule(
                GuidePresenterModule()).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        position = bundle.getInt(POSITION)
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {

        when (position) {
            0 -> {
                changeBtnVisibility(View.GONE)
                changeImageResource(R.mipmap.ic_guide_image_one)
            }

            1 -> {
                changeBtnVisibility(View.GONE)
                changeImageResource(R.mipmap.ic_guide_image_two)
            }

            2 -> {
                changeBtnVisibility(View.VISIBLE)
                changeImageResource(R.mipmap.ic_guide_image_three)
            }

            else -> mContext.logger_e(TAG, " position = $position")
        }

    }

    override fun initListener() {
        btnEnter.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                presenter?.setIsFirstTimeIsFalse()
                openLoginActivity()
            }
        })
    }


    override fun changeBtnVisibility(visibility: Int) {
        btnEnter.visibility = visibility
    }

    override fun changeImageResource(resId: Int) {
        ivGuide.setImageResource(resId)
    }

    override fun openLoginActivity() {
        mContext.openActivity(LoginActivity::class.java, false)
        val activity = mContext as? GuideActivity ?: return
        activity.finish()
    }
}